package com.example.front.controller;

import com.example.common.dto.JsonResult;
import com.example.common.dto.ListDto;
import com.example.common.dto.Pagination;
import com.example.front.dto.EventCreateDto;
import com.example.front.dto.EventDto;
import com.example.front.dto.EventSearchDto;
import com.example.front.entity.Event;
import com.example.front.serivce.EventService;
import com.example.interceptor.UserContext;
import com.example.passport.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "按钮盒事件管理")
@RestController
@CrossOrigin
@RequestMapping("/front/event")
public class EventController {
    @Resource
    EventService eventService;

    @ApiOperation("添加事件")
    @PostMapping("/create")
    public JsonResult<String> create(@Valid EventCreateDto eventCreateDto) {
        User user = UserContext.getUser();
        Event event = new Event();
        BeanUtils.copyProperties(eventCreateDto, event);
        event.setTenantId(user.getTenantId());
        eventService.createEvent(event);
        return new JsonResult<>(true, "添加成功");
    }

    @ApiOperation("删除事件")
    @PostMapping("/delete")
    public JsonResult<String> delete(Long id){
        if(id==null || id<=0){
            return new JsonResult<>(false,"请输入id");
        }
        User user = UserContext.getUser();
        Event event = Event.builder().id(id).tenantId(user.getTenantId()).build();
        eventService.deleteEvent(event);
        return new JsonResult<>(true, "删除成功");
    }

    @ApiOperation("修改事件")
    @PostMapping("/update")
    public JsonResult<String> update(@Valid EventDto eventDto){
        User user = UserContext.getUser();
        Event event = new Event();
        BeanUtils.copyProperties(eventDto, event);
        event.setTenantId(user.getTenantId());
        eventService.updateEvent(event);
        return new JsonResult<>(true, "修改成功");
    }

    @ApiOperation("列出所有事件")
    @GetMapping("/list")
    public JsonResult<ListDto<EventDto>> list(EventSearchDto condition, Pagination pagination){
        User user = UserContext.getUser();
        condition.setTenantId(user.getTenantId());

        return new JsonResult<>(eventService.listEvent(condition,pagination));
    }
}
